<template>
  <div class="articaldetail">
    <myheader title="文章详情">
      <div slot="left">
        <span
          class="iconfont iconjiantou back"
          @click="$router.back()"
          slot="left"
        ></span>
        <span class="iconfont iconnew new"></span>
      </div>
      <span
        :class="{ com: obj.has_follow }"
        @click="getfollow"
        slot="right"
        class="right"
        >{{ obj.has_follow ? "已关注" : "关注" }}</span
      >
    </myheader>
    <div class="detail">
      <div class="title">{{ obj.title }}</div>
      <div class="desc">
        <span>{{ obj.user.nickname }}</span> &nbsp;&nbsp;
        <span>{{ obj.create_date | date_dayformat }}</span>
      </div>
      <div class="content" v-html="obj.content" v-if="obj.type == 1"></div>

      <!--   播放器的基本属性配置 
      controls:是否显示播放器控制面板，如果没有控制面板，则播放器不可见
      src:视频文件的路径
      poster:设置第一帧的画面--封面
      autoplay:自动播放，谷歌不支持
      loop:循环播放-->
      <video
        class="video"
        :src="axios.defaults.baseURL + obj.content"
        controls
        poster="@/assets/u=2972938082,4088281575&fm=26&gp=0.jpg"
        v-if="obj.type == 2"
      ></video>
      <div class="opt">
        <span class="like">
          <van-icon
            name="good-job"
            :class="{ l: obj.has_like }"
            @click="getlike"
          />
          {{ obj.like_length || 0 }}
        </span>
        <span class="wechat"> <van-icon name="wechat" class="w" /> 微信</span>
      </div>
    </div>
    <!-- 精彩跟帖 -->
    <div class="keeps">
      <h2>精彩跟帖</h2>
      <div class="item" v-if="(onoff = v)">
        <div class="box">
          <img :src="url + v.user.head_img" />
          <div class="center">
            <p>{{ v.user.nickname }}</p>
            <span>{{ v.create_date | relativeformat }}</span>
          </div>
          <span @click="tocomment(v.id)" class="butt">回复</span><br /><br />
        </div>
        <div class="content">{{ v.content }}</div>
        <mycom v-if="v.parent" :v="v.parent"></mycom>
      </div>
      <div v-if="!onoff" class="oncom">暂时没有评价</div>
      <div class="more" @click="$router.push({ path: `/mythread/${obj.id}` })">
        更多跟帖
      </div>
    </div>
    <mycombut :obj="obj"></mycombut>
  </div>
</template>

<script>
import mycombut from "@/components/commentbuttom.vue";
import myheader from "@/components/hmheader.vue";
import axios from "@/utils/myaxios.js";
import { postarticaldetail } from "@/apis/postlists.js";
import { date_dayformat, relativeformat } from "@/utils/myfilters.js";
import { userunfollows, userfollows } from "@/apis/user";
import { getuserstar, postcomment } from "@/apis/postlists";
import mycom from "@/components/hmcomment.vue";
import { Toast } from "vant";
export default {
  data() {
    return {
      axios,
      obj: {
        user: {},
      },
      v: {
        user: {},
      },
      url: axios.defaults.baseURL,
      onoff: false,
    };
  },
  components: {
    myheader,
    mycombut,
    mycom,
  },
  filters: {
    date_dayformat,
    relativeformat,
  },
  async mounted() {
    setTimeout(() => {
      window.scrollTo(0, 0);
    }, 0);
    //获取对应id的数据进行渲染
    let res = await postarticaldetail(this.$route.params.id);
    // console.log(res);
    this.obj = res.data.data;
    let res2 = await postcomment(this.$route.params.id);
    this.comobj = res2.data.data;
    if (this.comobj) {
      this.v = this.comobj[0];
    }
  },
  methods: {
    async tocomment(id) {},

    // 关注与取消关注
    async getfollow() {
      try {
        let res;
        if (this.obj.has_follow) {
          //关注时取消关注
          res = await userunfollows(this.obj.user.id);
        } else {
          //添加关注
          res = await userfollows(this.obj.user.id);
        }
        //点击关注取反,刷新页面
        this.obj.has_follow = !this.obj.has_follow;
        Toast.success(res.data.message);
      } catch (error) {
        Toast.fail(error);
      }
    },
    async getlike() {
      try {
        //点赞时取消点赞
        let res = await getuserstar(this.obj.user.id);
        // console.log(res);
        if (this.obj.has_like) {
          //已点赞在点击时为取消点赞 数量-1
          this.obj.like_length--;
        } else {
          ++this.obj.like_length;
        }
        //点击点赞取反,刷新页面
        this.obj.has_like = !this.obj.has_like;
        Toast.success(res.data.message);
      } catch (error) {
        Toast(error);
      }
    },
  },
};
</script>

<style lang="less" scoped>
.oncom {
  width: 100%;
  border-top: 1px solid #ccc;
  text-align: center;
  padding: 20px 0;
  color: #888;
}
.butt {
  span {
    height: 20px;
    background-color: #eee;
    font-size: 13px;
    border-radius: 30px;
    line-height: 20px;
    padding: 2px 15px;
  }
}
.video {
  width: 100%;
  margin-bottom: 15px;
}
.right {
  padding: 5px 15px;
  background-color: #f00;
  color: #fff;
  text-align: center;
  border-radius: 15px;
  font-size: 13px;
  width: 50px;
}
.com {
  background: #fff;
  border: 1px solid #ccc;
  border-radius: 15px;
  color: #000;
}
.detail {
  padding: 15px;
  .title {
    font-size: 18px;
    font-weight: bold;
    padding: 10px 0;
  }
  .desc {
    line-height: 30px;
    color: #999;
    font-size: 13px;
  }
  .content {
    text-indent: 2em;
    line-height: 24px;
    font-size: 15px;
    padding-bottom: 30px;
    width: 100%;
    /deep/img {
      width: 100%;
      display: block;
    }
  }
}
.opt {
  display: flex;
  justify-content: space-around;
  .like,
  .wechat {
    height: 25px;
    padding: 0 15px;
    font-size: 14px;
    line-height: 25px;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 15px;
  }
  .l {
    color: red;
    font-size: 16px;
  }
  .w {
    color: rgb(84, 163, 5);
    font-size: 16px;
  }
}
.keeps {
  border-top: 5px solid #ddd;
  padding: 0 15px;
  margin: 0 auto;
  > h2 {
    line-height: 50px;
    text-align: center;
  }
  .item {
    padding: 10px 0;
    border-bottom: 1px solid #ccc;
    .box {
      padding: 10px 0;
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-size: 12px;
      color: #888;
      > img {
        display: block;
        width: 50/360 * 100vw;
        height: 50/360 * 100vw;
        border-radius: 50%;
        padding: 0 10px;
      }
      > .center {
        flex: 1;
        > p {
          line-height: 30px;
          font-size: 14px;
        }
      }
      .butt {
        height: 20px;
        background-color: #eee;
        font-size: 13px;
        border-radius: 30px;
        line-height: 20px;
        padding: 2px 15px;
      }
    }
    .content {
      text-indent: 1em;
    }
  }
  .more {
    width: 100px;
    margin: 20px auto;
    line-height: 30px;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 15px;
    color: #000;
  }
}
.back {
  position: absolute;
  top: 15px;
  left: 15px;
  display: inline-block;
}
.iconnew {
  color: rgb(197, 47, 36);
  margin-left: 20px;
  font-size: 50px;
}
</style>